package com.all.security.controller;

import com.all.security.model.request.LoginRequest;
import com.all.security.model.vo.LoginVO;
import com.all.security.response.ResponseMessage;
import com.all.security.response.ResponseMessageFactory;
import com.all.security.service.LoginService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

/**
 * @Author
 * @ClassName LoginController
 * @Date 2024/3/30 17:23
 * @Description TODO
 * @Version 1.0
 */
@RestController
@RequiredArgsConstructor
@Slf4j
public class LoginController {

    private final LoginService loginService;

    @PostMapping("/login")
    public ResponseMessage<LoginVO> login(@RequestBody LoginRequest loginRequest) {
        log.info("登陆 ===>>>");
        return ResponseMessageFactory.getSuccessResponseMessage(loginService.doLogin(loginRequest));
    }

    @GetMapping("/context")
    @PreAuthorize("hasAuthority('superadmin')")
    public String context(){
        SecurityContext context = SecurityContextHolder.getContext();
        log.info("用户名：{}",context.getAuthentication().getName());
        log.info("用户权限：{}",context.getAuthentication().getAuthorities());
        log.info("用户是否登录：{}",context.getAuthentication().isAuthenticated());
        log.info("用户证书：{}",context.getAuthentication().getCredentials());
        log.info("用户地址ip：{}",context.getAuthentication().getDetails());
        log.info("用户信息：{}",context.getAuthentication().getPrincipal());
        return "context：" + context;
    }
}
